package com.shujia.mapreduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

public class Demo3Filter {


    /**
     * 读取学生表  取出性别为男的学生
     * <p>
     * 在map端直接将数据写入hdfs
     */

    static class FilterMap extends Mapper<LongWritable, Text, NullWritable, Text> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            String line = value.toString();
            String gender = line.split(",")[3];

            if (gender.equals("男")) {

                //直接将护具写入hdfs
                context.write(NullWritable.get(), value);
            }
        }
    }


    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        Configuration conf = new Configuration();

        Job job = Job.getInstance(conf);

        job.setJobName("Demo3Filter");

        job.setJarByClass(Demo3Filter.class);

        job.setMapperClass(FilterMap.class);

        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(Text.class);

        //指定输入输出路径
        TextInputFormat.addInputPath(job, new Path("/data/student"));
        TextOutputFormat.setOutputPath(job, new Path("/data/out_filter"));

        job.waitForCompletion(true);

    }
}
